package com.example.module_house.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.module_house.R
import com.example.module_house.model.HouseEntity


class HouseAdapter : RecyclerView.Adapter<HouseAdapter.HouseViewHolder>() {
    private var buildings: List<HouseEntity> = emptyList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HouseViewHolder {
        return HouseViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_house, parent, false)
        )
    }

    override fun getItemCount(): Int {
        return buildings.size
    }

    fun submitList(list: List<HouseEntity>) {
        this.buildings = list
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: HouseViewHolder, position: Int) {
        holder.bind(buildings[position])
    }


    inner class HouseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val area: TextView = itemView.findViewById(R.id.area)
        private val status: TextView = itemView.findViewById(R.id.status)
        private val name: TextView = itemView.findViewById(R.id.name)
        private val mianji: TextView = itemView.findViewById(R.id.mianji)

        fun bind(item: HouseEntity) {
            area.text = "${item.id}室" // 或者使用其他字段
            name.text = "企业：${item.enterpriseName}"
            mianji.text = "面积：${item.area}m²"

            if (item.rented) {
                status.text = "出租"
                status.setBackgroundResource(R.drawable.bg_tv4)
                status.setTextColor(Color.parseColor("#166534"))
            } else {
                status.text = "已租"
                status.setBackgroundResource(R.drawable.bg_tv5)
                status.setTextColor(Color.parseColor("#991B1B"))
            }
        }

    }

}







